//---------------------------------------------------------------------------
/**
 * \class CBlockStream
 * \brief This is a block stream to provide assignBlock() and adjust_seekg()
 * \version 0.1
 *
 * \author Woo-Hyun Kim (woorung@nhncorp.com, woorung@gmail.com)
 * \author http://blog.naver.com/wisereign .
 *
 * \date 2007/12/05
 */
//---------------------------------------------------------------------------
#ifndef CBlockStreamH
#define CBlockStreamH
//---------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "stream_common.h"
#include "CBufferedStream.h"
#include "CBufferedLineStream.h"
//---------------------------------------------------------------------------
#define END_OF_LINE '\n'
//---------------------------------------------------------------------------
//#define KEY_BEGIN_PATTERN "<<<"
//#define KEY_END_PATTERN ">>>"
//#define VALUE_BEGIN_PATTERN "<<<"
//#define VALUE_END_PATTERN "<<<"
//---------------------------------------------------------------------------
#define DEFAULT_DELIMITER " \t\n"
//---------------------------------------------------------------------------
using namespace std;
//---------------------------------------------------------------------------
class CBlockStream {
private :
	char eol; // the end of line

	// if stdin stream, no free
	bool use_file_stream;

	//CBufferedStream *buf;
	CBufferedLineStream *buf;
	istream *block_stream;

	CBlockStream(const CBlockStream &rhs);
	CBlockStream &operator=(const CBlockStream &rhs);

protected:
	void seekg(long pos, ios::seekdir dir);

public :
	CBlockStream(const char *filename);
	CBlockStream(stringbuf *strbuf);
	CBlockStream(streambuf *stdbuf);
	virtual ~CBlockStream();

	long seekg(long pos);
	long adjust_seekg(long pos);
	long tellg();
	long size();
	void clear();

	void resize(int buf_size);

	long assignBlock(long block, long blocks);

	long getline(vector< pair<string,string> > &kv_pairs, char delim, long limit);
	long getline(vector< vector<string> > &kv_vectors, char delim, long limit);

	long getline(vector< pair<string,string> > &kv_pairs, char delim, int kpos, long limit);

	// get by line
	long getline(char *line, size_t size);
	long getline(char *line, size_t size, char delim);
	long getline(char *line, size_t size, char *delim);
};
//---------------------------------------------------------------------------
#endif
